import 'package:flutter/cupertino.dart';
import 'package:flutter_learn_vi/src/bean/bean_mine_uestion.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';

import '../../utils/navigator_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/11.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class MineQuestionModel {
  Future<bool> getQuestion() async {
    //post请求发送
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.mineQuestion,
    );

    if (responseInfo.success) {
      List<MineQuestionBean> questionList = [];

      List list = responseInfo.data["questionList"];
      if (list != null) {
        list.forEach((element) {
          questionList.add(MineQuestionBean.fromJson(element));
        });
      }

      UserHelper.getInstance.questionList = questionList;
    } else {
      LogUtils.e("获取题库信息失败");
    }
  }

  ///获取学习模版
  Future<StudyModelBean> getStudyModel({BuildContext context}) async {
    //post请求发送
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.canuseModel,
    );

    if (responseInfo.success) {
      List list = responseInfo.data;
      if (list.length == 1) {
        return StudyModelBean.fromJson(list[0]);
      } else {
        return StudyModelBean.fromJson(list[1]);
      }
      return null;
    } else {
      if (context != null) {
        if (responseInfo.code == 401) {
          //关闭首页面
        } else {
          NavigatorUtils.pop(context);
        }
      }
      LogUtils.e("获取学习模版信息失败");
      return null;
    }
  }

  Future<bool> getStartPic() async {
    ResponseInfo partResponseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.bootpage,
    );
    if (partResponseInfo.success) {
      List requestList = partResponseInfo.data;
      List<String> startUrlList = [];
      requestList.forEach((element) {
        startUrlList.add(element.toString());
      });
      UserHelper.getInstance.startUrlList = startUrlList;
    }
  }
}
